package cn.exrick.xboot.modules.masterdata.call;

import cn.exrick.xboot.modules.masterdata.server.MasterDataUpdate;
import cn.exrick.xboot.modules.masterdata.server.getData.IMdmSubscribe;
import cn.exrick.xboot.modules.masterdata.server.getData.MdmSubscribeServiceLocator;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.dom4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 测试
 */
@Slf4j
@RestController
@Api(description = "后台调用对应子系统接口")
@RequestMapping("/xboot/CallMasterGetData")
@Transactional
public class CallMasterGetDataController {
    @Autowired
    private MasterDataUpdate masterDataUpdate;

    @RequestMapping(value = "/getData", method = RequestMethod.POST)
    @ApiOperation(value = "getData")
    public String getData() {
        String data=null;
        try {
            MdmSubscribeServiceLocator locator = new MdmSubscribeServiceLocator();
            //MdmSubscribeServiceSoapBindingStub m = new MdmSubscribeServiceSoapBindingStub();
            IMdmSubscribe mdmSubscribeServicePort = locator.getMdmSubscribeServicePort();
             data = mdmSubscribeServicePort.getData("PORTAL", getDataXML());
            System.out.println(data);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
String xmltest="<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "" +
        "<persons> " +
        "  <person gender=\"男性\"> " +
        "    <name>张三</name>  " +
        "    <age>26</age>  " +
        "    <email>zhangsan@gmail.com</email>  " +
        "    <englishName>zhangsan</englishName>  " +
        "    <website>zhangsan.com</website> " +
        "  </person>  " +
        "  <person gender=\"男性\"> " +
        "    <name>李四</name>  " +
        "    <age>27</age>  " +
        "    <email>lisi@gmail.com</email>  " +
        "    <englishName>lisi</englishName>  " +
        "    <website>lisi.com</website> " +
        "  </person>  " +
        "  <person gender=\"男性\"> " +
        "    <name>王五</name>  " +
        "    <age>27</age>  " +
        "    <email>wangwu@gmail.com</email>  " +
        "    <englishName>wangwu</englishName>  " +
        "    <website>wangwu.com</website> " +
        "  </person> " +
        "</persons>";
        test(xmltest);
        return data;
    }
    public static void test(String data){
        List<HashMap<String, String>> personList = new ArrayList<HashMap<String, String>>();
        try {
            Document document = DocumentHelper.parseText(data);
            Element rootElement = document.getRootElement();// 获取根节点
            for (Iterator<?> iterator = rootElement.elementIterator(); iterator.hasNext();) {
                Element element = (Element) iterator.next();

                Map<String, String> personMap = new HashMap<String, String>();
                Attribute genderAttr = element.attribute("gender");//获取元素的gender属性对象
                personMap.put("gender", StringUtils.trim(genderAttr.getValue()));// 获取属性对象的值并插入map中，键名为gender

                for (Iterator<?> iterator2 = element.elementIterator(); iterator2.hasNext();) {
                    Element element2 = (Element) iterator2.next();

                    personMap.put(element2.getName(), element2.getTextTrim());// 将子节点的名称和值分别插入map中
                }
                personList.add((HashMap<String, String>) personMap);//将map插入list中
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        System.out.println("集合长度："+personList.size());
        System.out.println(personList);

    }




    public static String getDataXML( ){
        String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                +"  <query>"
                +"  <pageSize>2</pageSize>"
                +"  <currentPage>2</currentPage>"
                +"  <totalRecord>0</totalRecord>"
                +"  </query> ";
        return soapXML;
    }

    @RequestMapping(value = "/masterDataTest", method = RequestMethod.POST)
    @ApiOperation(value = "getData")
    public String masterDataTest() {
        masterDataUpdate.dataUpdate();
        return "";
    }



    /*public static void main(String[] argv) {
        try {
            MdmSubscribeServiceLocator locator = new MdmSubscribeServiceLocator();
            //MdmSubscribeServiceSoapBindingStub m = new MdmSubscribeServiceSoapBindingStub();
            IMdmSubscribe mdmSubscribeServicePort = locator.getMdmSubscribeServicePort();
            String data = mdmSubscribeServicePort.getData("PORTAL", getDataXML());
            System.out.println(data);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }*/


}
